//
//  SelectCoinView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
protocol SelectCoinDelegate:class {
    func selectIndex(index:Int)
}
class SelectCoinView: UIView, UITableViewDataSource, UITableViewDelegate{
    var dataSource: SelectCoinViewController!
    var selectIndex:Int = 0
    weak var delegate:SelectCoinDelegate?
    lazy var dateArr:Array<AssetModel> = {
        let array:Array<AssetModel> = []
        return array
    }()
    lazy var tableView:UITableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.grouped)
    init(dataSource: SelectCoinViewController) {
        super.init(frame: CGRect.zero)
        
        self.dataSource = dataSource
        self.themeBackground = ThemeManager.ThemeColorKeys.background
        self.setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() -> Void {
        self.addSubview(tableView)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.backgroundColor = UIColor.clear
        tableView.delegate = self
        tableView.dataSource = self
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalToSuperview()
        }
    }
    
    //MARK tableViewDelegate
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dateArr.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:BaseTableViewCell = BaseCellFactory .createTableViewCellWithModel(model: BaseModel(), tableView: tableView, style: .BaseStyleCell)
        cell.nameLabel.snp.remakeConstraints { (make) in
            make.left.equalToSuperview().offset(46)
            make.centerY.equalToSuperview()
        }
        cell.contentView.addSubview(cell.iconImage)
        cell.iconImage.layer.cornerRadius = 10
        cell.iconImage.layer.masksToBounds = true
        cell.iconImage.snp.makeConstraints({ (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
        })
        
        cell.contentView.addSubview(cell.selectImage)
        cell.selectImage.image = UIImage(named: "check_mark")
        cell.selectImage.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-24)
            make.centerY.equalToSuperview()
        }
        if selectIndex == indexPath.row {
            cell.selectImage.isHidden = false
        }else {
            cell.selectImage.isHidden = true
        }
        let model = dateArr[indexPath.row]
        cell.nameLabel.text = model.coinCode
        cell.iconImage.sd_setImage(with: URL(string: model.logoUrl), placeholderImage:UIImage(named: "coinIcon"))
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 0.1))
        return view
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        let data = dateArr[indexPath.row]
        selectIndex = indexPath.row
        tableView.reloadData()
        if delegate != nil {
            self.delegate?.selectIndex(index: self.archiveIndex(model: data, index: indexPath.row))
        }
    }
    
    func archiveIndex(model:AssetModel,index:Int) -> Int{
        if dataSource.sendType == .legalType || dataSource.receiveType == .legalType {
            let array = self.archiveDataSource()
            for (index,data) in array.enumerated() {
                if data.coinCode == model.coinCode {
                    return index
                }
            }
            return 0
        }else {
            return index
        }
    }
    
    func archiveDataSource() -> [AssetModel] {
        if dataSource.sendType == .legalType {
            return dataSource.legalArr
        }else if dataSource.sendType == .coinType {
            return dataSource.dateArr
        }else {
            return dataSource.leverArr
        }
    }
    
    func setData(array:Array<AssetModel>) {
        dateArr = array
        tableView.reloadData()
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
